$('#login_button2').click(function () {
    sub();
});
function sub() {
    var user = $.trim($("#mobile2").val());//获取用户名
    var pd = $.trim($("#password").val());

    var url = "/login"; // 接口 
    var params = { //HTMl数据格式
        'username': user,
        'password': pd,
    };
    $.ajax({
        type: "post",
        url: "http://lex.itdida.com/itdida-api/login",
        contentType: "application/x-www-form-urlencoded",
        data: params,
        dataType: 'json',
        success: function (data) {
            if (data.statusCode == 200) {
                sessionStorage.setItem('token', data.data);
                if (parent.$(".content_title ul.nav li:first").hasClass('active')) {
                    parent.login();
                    var index = parent.layer.getFrameIndex(window.name);
                    parent.layer.close(index);
                } else if (parent.$(".content_title ul.nav li:eq(1)").hasClass('active')) {
                    parent.login();
                    var index = parent.layer.getFrameIndex(window.name);
                    parent.layer.close(index);
                }

            }
        },
        error: function (data) {
            var msg = jQuery.parseJSON(data.responseText).data;
            layer.msg(msg, {
                offset: ['200px', '200px'],
                anim: 6
            });
        }
    })
}

$(function () {
    initView();
    $("#login_button2").click(function () {
        if ($(".save_account").is(":checked")) {
            setCookie("cookie_account", $("#mobile2").val());
            setCookie("cookie_password", $("#password").val());
            setCookie("rememberMe", true);
        } else {
            delCookie("cookie_account");
            delCookie("cookie_password");
            delCookie("rememberMe");
        }
    });
});

function initView() {
    if (getCookie("cookie_account")) {
        $("#mobile2").val(getCookie("cookie_account"));
    }
    if (getCookie("cookie_password")) {
        $("#password").val(getCookie("cookie_password"));
    }
    if (getCookie("rememberMe")) {
        $(".save_account").attr("checked", getCookie("rememberMe"));
    }
    $("#mobile2").focus(function () {
        this.select();
    });
    $("#password").focus(function () {
        this.select();
    });
}

/**
* 写入cookie
* @param name  cookie 名
* @param value  cookie 值
*/
function setCookie(name, value) {
    var Days = 30; //此 cookie 将被保存 30 天
    var exp = new Date();
    exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
    document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString();
}
/**
* 删除cookie
* @param name
*/
function delCookie(name) {
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval = getCookie(name);
    if (cval != null) document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();
}
/**
* 读取cookie
* @param name
* @returns
*/
function getCookie(name) {
    var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));
    if (arr != null)
        return unescape(arr[2]);
    return null;
}